public class Thermostat {

    // 定义水箱的状态
    enum State {
        HEATING, COOLING, EMPTY, MAINTENANCE, SLEEPING
    }

    private State state = State.SLEEPING; // 初始状态为休眠
    private double temperature = 20.0; // 初始温度为20°C
    private boolean hasWater = true; // 初始假设水箱中有水
    private boolean relayOn = false; // 继电器初始状态为关闭

    // 模拟时间变化，这里简化为手动调用
    private int hour = 7; // 初始时间为早上7点

    public void updateTemperature(double newTemperature) {
        temperature = newTemperature;
        checkTemperature();
    }

    public void updateWaterStatus(boolean newWaterStatus) {
        hasWater = newWaterStatus;
        checkWaterStatus();
    }

    public void checkTemperature() {
        switch (state) {
            case COOLING:
                if (temperature <= 20) {
                    state = State.EMPTY;
                    relayOn = false;
                    System.out.println("Temperature dropped to " + temperature + "°C, no water to heat.");
                }
                break;
            case HEATING:
                if (temperature >= 100) {
                    state = State.COOLING;
                    relayOn = false;
                    System.out.println("Temperature reached 100°C, heating stopped.");
                }
                break;
        }
    }

    public void checkWaterStatus() {
        switch (state) {
            case COOLING:
                if (hasWater) {
                    state = State.HEATING;
                    relayOn = true;
                    System.out.println("Water detected, starting to heat.");
                }
                break;
            case EMPTY:
                if (hasWater) {
                    state = State.COOLING;
                    System.out.println("Water filled, ready to heat.");
                }
                break;
            case SLEEPING:
                if (!isNight()) {
                    state = State.COOLING;
                    System.out.println("当前不是夜晚, Waking up, ready to heat.");
                }
                break;
        }
    }

    public void tick() {
        // 模拟时间流逝，这里简化为手动调用
        hour++;
        if (hour == 23) {
            state = State.SLEEPING;
            relayOn = false;
            System.out.println("当前时间 23 点, 到夜晚了, going to sleep.");
        } else if (hour == 7) {
            state = State.COOLING;
            System.out.println("当前时间 7 点, 到早上了, waking up.");
        }
    }

    private boolean isNight() {
        return hour >= 23 || hour < 7;
    }

    public static void main(String[] args) {
        Thermostat thermostat = new Thermostat();

        // 模拟一天中的温度变化和水量变化
        for (int i = 0; i < 24; i++) {
            thermostat.tick();
            if (i % 2 == 0) { // 每隔两小时改变温度
                double newTemperature = thermostat.getTemperature() + (i < 12 ? 5 : -5);
                thermostat.updateTemperature(newTemperature);
            }
            if (i == 12) { // 在中午时分，水量变为无
                thermostat.updateWaterStatus(false);
            }
            if (i == 18) { // 在傍晚时分，水量恢复
                thermostat.updateWaterStatus(true);
            }
        }
    }

    public double getTemperature() {
        return temperature;
    }
}